#include <tsin/library/TouhouAnimatedBulletSeries.hpp>

#include <tsin/library/BuiltinImages.hpp>

#include <tsin/util/Logger.hpp>

using namespace tsin;

const int TICKS_PER_FRAME = 5;

static void animation(DecoratedImage *image, int prevTick, int curTick)
{
        if (curTick == image->nrFrames()) {
                image->setFrame(0, false);
                curTick = 0;
        }

        int prevFrame = prevTick / TICKS_PER_FRAME;
        int curFrame = curTick / TICKS_PER_FRAME;
        if (prevFrame == curFrame)
                return;

        float l = image->left(), r = image->right(), w = image->width();
        int relative = curFrame - prevFrame;
        image->setXRange(l + relative*w, r + relative*w);
}

const char *TouhouAnimatedBulletSeries::getImagePath()
{
        switch(color) {
        case BLUE:
        case RED:
                return RES_PREFIX "bullet/etama2.png";
        case PINK:
        case YELLOW:
        case DARK_YELLOW:
                return RES_PREFIX "bullet/etama8.png";
        }
        return RES_PREFIX "bullet/etama.png";
}

int TouhouAnimatedBulletSeries::getSizeX()
{
        return 32;
}

int TouhouAnimatedBulletSeries::getSizeY()
{
        return 32;
}

int TouhouAnimatedBulletSeries::getCoordX()
{
        switch(color) {
        case RED:
        case BLUE:
        case DARK_YELLOW:
        case PINK:
                return 0;
        case YELLOW:
                return 128;
        }
        return 0;
}

int TouhouAnimatedBulletSeries::getCoordY()
{
        switch(color) {
        case RED:
                return 80;
        case BLUE:
                return 48;
        case YELLOW:
        case DARK_YELLOW:
                return 16;
        case PINK:
                return 96;
        }
        return 0;
}

BlendStyle TouhouAnimatedBulletSeries::getBlendStyle()
{
        return OVERPRINTING;
}

int TouhouAnimatedBulletSeries::getNrFrames()
{
        return 20;
}

NextFrameHandler TouhouAnimatedBulletSeries::getHandler()
{
        return animation;
}
